package com.yuandian.bpm.bi.modules.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.bpm.bi.modules.user.entity.ConfAddresseeUser;
import com.yuandian.bpm.bi.modules.user.service.ConfAddresseeUserService;
import com.yuandian.bpm.bi.modules.user.vo.ConfAddresseeUserVo;
import com.yuandian.bpm.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * 推送对象_用户-Controller
 * @author zfsn
 * @date 2023-7-3
 */
@Api(tags = "推送对象_用户功能接口")
@RestController
@RequestMapping("/addresseeUser")
public class ConfAddresseeUserController {

	@Autowired
	private ConfAddresseeUserService confAddresseeUserService;

	/**
	 * 通过主键查询数据
	 *
	 * @param id 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，不支持批量查询")
	@GetMapping("/{id}")
	public R<ConfAddresseeUser> queryByIds(@PathVariable("id") Integer id) {
		return R.ok(confAddresseeUserService.getOneById(id));
	}

	/**
	 * 分页查询
	 *
	 * @param
	 * @param confAddresseeUser 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<Page<ConfAddresseeUser>> listPage(@RequestBody @ApiIgnore ConfAddresseeUserVo confAddresseeUser) {
		return R.ok(confAddresseeUserService.listPage(confAddresseeUser));
	}

	/**
	 * 新增数据
	 *
	 * @param confAddresseeUser 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "nickName", value = "用户名称", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "phone", value = "手机号码", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "email", value = "邮箱账号", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "remark", value = "备注", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "enable", value = "是否启用", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	public R<String> save(@RequestBody @ApiIgnore ConfAddresseeUser confAddresseeUser) {
		return confAddresseeUserService.saveData(confAddresseeUser);
	}

	/**
	 * 通过主键更新数据
	 *
	 * @param confAddresseeUser 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "nickName", value = "用户名称", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "phone", value = "手机号码", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "email", value = "邮箱账号", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "remark", value = "备注", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "enable", value = "是否启用", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	public R<String> updateById(@RequestBody @ApiIgnore ConfAddresseeUser confAddresseeUser) {
		return confAddresseeUserService.updateDataById(confAddresseeUser);
	}

	/**
	 * 通过主键删除数据
	 *
	 * @param id 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@DeleteMapping("/delete/{id}")
	public R<String> removeById(@PathVariable("id") Integer id) {
		return confAddresseeUserService.removeDataById(id);
	}




	/**
	 * 返回码配置详情信息模板
	 * @param response  response
	 * @throws IOException
	 */
	@GetMapping("/downloadTemplate")
	@ApiOperation(value = "模板下载", notes = "模板下载")
	public void getTemplate(HttpServletResponse response) throws IOException {
		try {
			// 1、设置response
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			String fileName = URLEncoder.encode("推送对象-用户", "utf-8").replaceAll("\\+", "%20");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

			InputStream is = null;
			OutputStream os = null;
			try {
				// 2、获取文件输入流
				Resource resource = new ClassPathResource("excel/address_user.xlsx");
				is = resource.getInputStream();

				// 3、获取输出流
				os = new BufferedOutputStream(response.getOutputStream());

				// 4、写入
				int len;
				byte[] bytes = new byte[1024];
				while ((len = is.read(bytes)) != -1) {
					os.write(bytes, 0, len);
				}
				os.flush();
			} finally {
				// 5、关闭资源
				if (is != null) {
					is.close();
				}
				if (os != null) {
					os.close();
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			response.reset();
			response.setContentType("application/json");
			response.setCharacterEncoding("utf-8");
			try {
				response.getWriter().println(R.failed(
						null, "导出Excel文件失败！").toString());
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * 上传
	 * @param file file
	 * @return Result
	 * @throws IOException
	 */
	@ApiOperation(value = "导入", notes = "导入")
	@PostMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file) throws IOException {
		return confAddresseeUserService.upload(file);
	}

}
